<script lang="ts" setup>
import { computed, ref } from 'vue'
import { useChatStore } from '@/store/chat'
import { useUIStore } from '@/store/ui'
import ChatItem from './ChatItem.vue'
import SessionItem from './SessionItem.vue'

const chatStore = useChatStore()
const uiStore = useUIStore()

// 搜索关键词
const searchKeyword = ref('')

// 根据当前激活的工具栏项显示不同内容
const currentView = computed(() => uiStore.activeToolbarItem)

// 过滤后的会话列表 - 使用store方法
const filteredSessions = computed(() => {
  return chatStore.searchSessions(searchKeyword.value)
})

// 过滤后的联系人列表 - 使用store方法
const filteredContacts = computed(() => {
  return chatStore.searchContacts(searchKeyword.value)
})

// 处理添加按钮点击
const handleAddClick = (): void => {
  console.log('点击添加按钮')
  // 这里可以添加具体的逻辑，比如打开添加好友对话框
}

// 处理会话点击 - 直接使用store方法
const handleSessionClick = (sessionId: string): void => {
  chatStore.setActiveSession(sessionId)
  chatStore.markMessagesAsRead(sessionId)
}

// 处理联系人点击 - 创建会话并激活
const handleContactClick = (contactId: string): void => {
  // 检查是否已存在会话
  const existingSession = chatStore.sessions.find((s) => s.id === contactId)
  if (existingSession) {
    chatStore.setActiveSession(contactId)
  } else {
    // 创建新会话
    chatStore.createSession(contactId)
  }
}

// 获取功能名称的辅助方法
const getFeatureName = (featureId: string): string => {
  const featureMap: Record<string, string> = {
    favorites: '收藏',
    files: '文件传输助手',
    moments: '朋友圈',
    miniapps: '小程序',
    games: '游戏中心',
    plugins: '插件'
  }
  return featureMap[featureId] || '未知功能'
}
</script>

<template>
  <div class="sidebar">
    <!-- 搜索栏 -->
    <div class="toolbar">
      <div class="flex-grow-1 mr-2">
        <input v-model="searchKeyword" type="text" placeholder="搜索" class="input" />
      </div>
      <button
        class="btn btn-secondary btn-small"
        :title="currentView === 'chat' ? '添加聊天' : '添加联系人'"
        @click="handleAddClick"
      >
        +
      </button>
    </div>
    <!-- 内容区域 -->
    <div class="flex-grow-1 overflow-y-auto">
      <!-- 聊天列表 -->
      <div v-if="currentView === 'chat'" class="list">
        <SessionItem
          v-for="session in filteredSessions"
          :key="session.id"
          :session="session"
          :is-active="chatStore.activeSessionId === session.id"
          @click="handleSessionClick"
        />
      </div>
      <!-- 联系人列表 -->
      <div v-else-if="currentView === 'contacts'" class="list">
        <ChatItem
          v-for="contact in filteredContacts"
          :key="contact.id"
          :contact="contact"
          :is-selected="chatStore.activeSessionId === contact.id"
          @click="handleContactClick"
        />
      </div>

      <!-- 其他功能页面的占位 -->
      <div v-else class="h-100 d-flex align-items-center justify-content-center">
        <div class="text-center text-muted">
          <div class="fs-xxlarge mb-3 opacity-50">🚧</div>
          <div class="fs-medium">{{ getFeatureName(currentView) }}功能开发中...</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.sidebar {
  width: 300px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* 防止整体溢出 */
}

.toolbar {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-color);
}

.flex-grow-1 {
  flex-grow: 1;
}

.mr-2 {
  margin-right: 8px;
}

.input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background-color: var(--input-bg-color);
  color: var(--text-color);
  font-size: 14px;
}

.input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.btn {
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.btn-secondary {
  background-color: var(--secondary-color);
  color: var(--text-color);
}

.btn-secondary:hover {
  background-color: var(--secondary-hover-color);
}

.btn-small {
  padding: 6px 10px;
  font-size: 12px;
}

.list {
  flex-grow: 1;
  overflow-y: auto;
  overflow-x: hidden; /* 防止横向滚动 */
  width: 100%;
}

.h-100 {
  height: 100%;
}

.d-flex {
  display: flex;
}

.align-items-center {
  align-items: center;
}

.justify-content-center {
  justify-content: center;
}

.text-center {
  text-align: center;
}

.text-muted {
  color: var(--text-muted-color);
}

.fs-xxlarge {
  font-size: 48px;
}

.fs-medium {
  font-size: 16px;
}

.mb-3 {
  margin-bottom: 12px;
}

.opacity-50 {
  opacity: 0.5;
}
</style>
